package com.trkj.wiki.controller;

import com.trkj.wiki.pojo.Test;
import com.trkj.wiki.service.TestService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@Log4j2
public class TestController {
    @Value("${test.hello:TEST}")
    private String testHello;

    @Resource
    private RedisTemplate redisTemplate;

    @Autowired
    private TestService testService;


    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!"+testHello;
    }
    @RequestMapping("/hello/post")
    public String helloPost(String name){
        return "Hello World! Post,"+name;
    }

    @GetMapping("/test/list")
    public List<Test> list(){
        return testService.list();
    }

    @RequestMapping("redis/set/{key}/{value}")
    public String set(@PathVariable Long key,@PathVariable Long value){
        redisTemplate.opsForValue().set(key,value,3600, TimeUnit.SECONDS);
        log.info("key:{},value:{}",key,value);
        return "success";
    }

    @RequestMapping("redis/get/{key}")
    public Object set(@PathVariable Long key){
        Object object = redisTemplate.opsForValue().get(key);
        log.info("key:{},value:{}",key,object);
        return object;
    }
}
